package com.yunji.ylexpiration_backstage.controller;

import com.yunji.ylexpiration_backstage.entity.FurnAdmin;
import com.yunji.ylexpiration_backstage.entity.FurnCommodity;
import com.yunji.ylexpiration_backstage.entity.FurnOrders;
import com.yunji.ylexpiration_backstage.service.FurnAdminService;
import com.yunji.ylexpiration_backstage.service.FurnCommodityService;
import com.yunji.ylexpiration_backstage.service.FurnOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FurnAdminController {
    @Autowired
    private FurnAdminService furnAdminService;

    @Autowired
    private FurnCommodityService furnCommodityService;

    @Autowired
    private FurnOrdersService furnOrdersService;

    @PostMapping("index-2.html")
    public String dologin(FurnAdmin furnAdmin, HttpSession session, HttpServletRequest request){
        //登录
        Object admin = session.getAttribute("admin");
        if(admin==null) {
            FurnAdmin login = furnAdminService.login(furnAdmin, request, session);
            if (login != null) {
                session.setAttribute("admin", login);
                session.setMaxInactiveInterval(60 * 10);
            }
        }
        //热门商品
        List<FurnCommodity> furnCommodities = furnCommodityService.selectBysalesnumByLimit10();
        if(furnCommodities!=null&&furnCommodities.size()!=0){
            session.setAttribute("furnCommodities",furnCommodities);
        }

        //待发货
        List<FurnOrders> furnOrders = furnOrdersService.selectBystaticBylimit10();
        if(furnOrders!=null&&furnOrders.size()!=0){
            session.setAttribute("furnOrders",furnOrders);
        }

        return "index-2";
    }

    @GetMapping("loginadmin.html")
    public String loginadmin(HttpSession session){
        session.invalidate();
        return "loginadmin";
    }

}
